#include "stdafx.h"
#include "Resolution.h"
#include "Engine.h"

Graphics::Resolution::Resolution()
{

}

Graphics::Resolution::~Resolution()
{

}

void Graphics::Resolution::Init(const Vector2f& aResolution)
{
	myBaseResolution = Vector2f(1280.0f, 720.0f);
	myResolution = aResolution;
	mySizeScale = aResolution.x / myBaseResolution.x;
}

const Vector2f Graphics::Resolution::ConvertToRenderPos(const Vector2f& aOriginalPosition)
{
	Vector2f returnValue((2.0f*aOriginalPosition.x/1280.0f) - 1.0f, -((2.0f*aOriginalPosition.y/720.0f) - 1.0f));
	float usedResolutionY = myResolution.x * (myBaseResolution.y / myBaseResolution.x);
	usedResolutionY /= myResolution.y;
	returnValue.y *= usedResolutionY;
	return returnValue;
}

const Vector2f Graphics::Resolution::ConvertToRenderSize(const Vector2f& aOriginalSize)
{
	Vector2f returnValue(aOriginalSize);
	returnValue /= myBaseResolution;

	float usedResolutionY = myResolution.x * (myBaseResolution.y / myBaseResolution.x);
	usedResolutionY /= myResolution.y;
	returnValue.y *= usedResolutionY;

	return returnValue;
}

const Vector2f Graphics::Resolution::ConvertToCursorPos(const Vector2f& aOriginalPosition)
{
	Vector2f returnValue(aOriginalPosition);
	returnValue /= myResolution;
	returnValue *= myBaseResolution;

	float usedResolutionY = myResolution.x * (myBaseResolution.y / myBaseResolution.x);
	usedResolutionY /= myResolution.y;
	returnValue.y /= usedResolutionY;

	return returnValue;
}

void Graphics::Resolution::SetupScissorRect()
{
	float usedResolutionY = myResolution.x * (myBaseResolution.y / myBaseResolution.x);
	float blackSpace = myResolution.y - usedResolutionY;
	blackSpace /= 2.0f;


	RECT screenArea;
	screenArea.left = 0;
	screenArea.top = static_cast<LONG>(blackSpace);
	screenArea.bottom = static_cast<LONG>(myResolution.y - blackSpace);
	screenArea.right = static_cast<LONG>(myResolution.x);
	HRESULT hr = Engine::GetDevice()->SetScissorRect(&screenArea);

	if(FAILED(hr))
	{
		DL_ERROR("Failed to set scissor rectangle");
	}
	else
	{
		DL_PRINT("Scissor rectangle set");
	}

	DL_INCREASETAB;
	DL_PRINT(ExtendedString("Left: ") << screenArea.left);
	DL_PRINT(ExtendedString("Right: ") << screenArea.right);
	DL_PRINT(ExtendedString("Top: ") << screenArea.top);
	DL_PRINT(ExtendedString("Bottom: ") << screenArea.bottom);
	DL_DECREASETAB;
}

const Vector2f Graphics::Resolution::GetCenter()
{
	return myBaseResolution / 2.0f;
}

const Vector2f& Graphics::Resolution::GetBaseResolution()
{
	return myBaseResolution;
}